﻿/*++

Copyright (c) 2011 Rainr

Module Name:

	Rainr.Utilities.Common.RandomEx : System.Random

Abstract:

	随机数扩展类

Author:

	Zhou Junyu (Rainr)    2011-05-05

Revision History:
  
--*/

using System;
using System.Collections.Generic;

namespace Rainr.Utilities.Common
{
    /// <summary>
    /// 随机数扩展类
    /// </summary>
    public class RandomEx : Random
    {
        #region 构造方法
        public RandomEx() : base() { }
        public RandomEx(int Seed) : base(Seed) { }
        #endregion

        #region 实例成员
        /// <summary>
        /// 返回一个大于等于minValue且小于maxValue的永远不回重复的随机数，如果取得数数量大于范围值个数则将会重复
        /// </summary>
        /// <param name="minValue">返回结果大于或等于的最小值</param>
        /// <param name="maxValue">返回结果小于的最大值</param>
        /// <returns></returns>
        public int NextEx(int minValue, int maxValue)
        {
            int resultValue = 0;
            int maxminValue = minValue - minValue;
            do
            {
                if (this.RecordedDoubleValues.Count.Equals(maxminValue))
                {
                    lock (this.recordedIntValues)
                    {
                        this.recordedIntValues.Clear();
                    }
                }
                resultValue = base.Next(minValue, maxValue);
            } while (this.RecordedIntValues.Contains(resultValue));
            lock (this.recordedIntValues)
            {
                this.recordedIntValues.Add(resultValue);
            }
            return resultValue;
        }
        /// <summary>
        /// 返回一个大于等于零且小于maxValue的永远不会重复的随机数，如果取得数数量大于范围值个数则将会重复
        /// </summary>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public int NextEx(int maxValue)
        {
            return this.NextEx(0, maxValue);
        }
        /// <summary>
        /// 返回一个大于等于零且小于Int32.MaxValue的永远不会重复的随机数，如果取得数数量大于范围值个数则将会重复
        /// </summary>
        /// <returns></returns>
        public int NextEx()
        {
            return this.NextEx(0, Int32.MaxValue);
        }

        /// <summary>
        /// 返回一个永远不回重复的大于等于0.0且小于1.0之间的数，如果取得数数量大于范围值个数则将会重复
        /// </summary>
        /// <returns></returns>
        public double NextDoubleEx()
        {
            double resultValue = 0;
            do
            {
                if (this.RecordedDoubleValues.Count.Equals(10))
                {
                    lock (this.recordedDoubleValues)
                    {
                        this.recordedDoubleValues.Clear();
                    }
                }
                resultValue = base.NextDouble();
            } while (this.RecordedDoubleValues.Contains(resultValue));
            lock (this.recordedDoubleValues)
            {
                this.recordedDoubleValues.Add(resultValue);
            }
            return resultValue;
        }

        /// <summary>
        /// 返回一个随机的字节数组
        /// </summary>
        /// <param name="byteLenght">字节数组长度</param>
        /// <returns>随机的字节数组</returns>
        public byte[] NextBytes(uint byteLenght)
        {
            byte[] resultBytes = new byte[byteLenght];
            this.NextBytes(resultBytes);
            return resultBytes;
        }

        private List<int> RecordedIntValues
        {
            get
            {
                if (null == this.recordedIntValues) this.recordedIntValues = new List<int>();
                return this.recordedIntValues;
            }
        }
        private List<double> RecordedDoubleValues
        {
            get
            {
                if (null == this.recordedDoubleValues) this.recordedDoubleValues = new List<double>();
                return this.recordedDoubleValues;
            }
        }
        private List<int> recordedIntValues;
        private List<double> recordedDoubleValues;

        #endregion


    }
}
